/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bash.carrep.ui;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;


/**
 * Le footer standard. Incluant des boutons pour les tâches recurrentes telles
 * que Edit, Save, Cancel.
 * @author bash
 */
public class EditSaveCancelView<T> extends HorizontalLayout implements ClickListener {

    BeanItemContainer<T> datasource;
    Button edit, save, cancel,delete,anew;
    AbstractFrom form;
    private boolean newMode=false;
    private T newObject = null;

    public EditSaveCancelView() {
    }

    public EditSaveCancelView(BeanItemContainer<T> datasource,AbstractFrom form) {
        this.datasource = datasource;
        this.form=form;

        edit = new Button("Editer", this);
        save = new Button("Enregistrer", this);
        cancel = new Button("Annuler", this);
        delete=new Button("Supprimer",this);
        anew=new Button("Nouveau",this);
        
        addComponent(edit);
        addComponent(save);
        addComponent(cancel);
         addComponent(delete);
        addComponent(anew);
        
        setFieldsReadOnly(true);
        
    }

    public BeanItemContainer<T> getDatasource() {
        return datasource;
    }

    public void setDatasource(BeanItemContainer<T> datasource) {
        this.datasource = datasource;
    }

    @Override
    public void buttonClick(ClickEvent event) {
       Button source = event.getButton();

		if (source == save) {
			/* If the given input is not valid there is no point in continuing */
			if (!form.isValid()) {
				return;
			}

			form.commit();
                        
                        form.submit();
                        
			if (newMode) {
				/* We need to add the new pojo to the container */
				Item addedItem = getDatasource().addItem(newObject);
				/*
				 * We must update the form to use the Item from our datasource
				 * as we are now in edit mode (no longer in add mode)
				 */
				form.setItemDataSource(addedItem);

				newMode = false;
			}
			setFieldsReadOnly(true);
                        
		} else if (source == cancel) {
			if (newMode) {
				newMode = false;
				/* Clear the form and make it invisible */
				form.setItemDataSource(null);
			} else {
				form.discard();
			}
			setFieldsReadOnly(true);
		} else if (source == edit) {
			setFieldsReadOnly(false);
		}else if(source==anew){
                    setFieldsReadOnly(false);
                   form.setItemDataSource(new BeanItem(form.pojo));
                }
    }
  
  
	public void setFieldsReadOnly(boolean readOnly) {
		form.setReadOnly(readOnly);
		save.setVisible(!readOnly);
		cancel.setVisible(!readOnly);
                delete.setVisible(!readOnly);
		edit.setVisible(readOnly);
	}

  
}
